package com.woniuxy.lrz.carlproject.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.serviceform.AddServiceForm;
import com.woniuxy.lrz.carlproject.controller.form.serviceform.UpdateServiceForm;
import com.woniuxy.lrz.carlproject.dao.ServiceInfoMapper;
import com.woniuxy.lrz.carlproject.model.ServiceInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.model.ServiceSecondLevel;
import com.woniuxy.lrz.carlproject.service.ServiceInfoService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo> implements ServiceInfoService {

    @Resource
    private ServiceInfoMapper serviceInfoMapper;
    @Override
    public ServiceInfo getServiceById(Integer serviceId) {
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceId);
        if (serviceInfo == null) {
            throw  new AppException("查询失败");
        }
        return serviceInfo;
    }

    @Override
    public Page<ServiceInfo> getAll(int current, int size) {
        Page<ServiceInfo> serviceInfoPage = serviceInfoMapper.selectPage(new Page<>(current, size), null);
        if (serviceInfoPage==null){
            throw  new AppException("查询服务详情失败");
        }
        return serviceInfoPage;
    }

    @Override
    public void add(AddServiceForm addServiceForm) {
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("service_name",addServiceForm.getServiceName());
        ServiceInfo one = serviceInfoMapper.selectOne(wrapper);
        if (one!=null){
            throw  new AppException("该服务名已经存在");
        }
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setServiceName(addServiceForm.getServiceName());
        serviceInfo.setServicePrice(addServiceForm.getServicePrice());
        serviceInfo.setServicePhoto(addServiceForm.getServicePhoto());
        serviceInfo.setServiceSecondId(addServiceForm.getServiceSecondId());
        serviceInfo.setServiceStatus("UP");
        serviceInfo.setDeleted(0);
        int insert = serviceInfoMapper.insert(serviceInfo);
        if (insert<1){
            throw  new AppException("添加服务失败");
        }
    }

    @Override
    public void updateService(UpdateServiceForm updateServiceForm) {
//        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
//        wrapper.eq("service_name",updateServiceForm.getServiceName());
//        ServiceInfo one = serviceInfoMapper.selectOne(wrapper);
//        if (one!=null){
//            throw  new AppException("该服务名已经存在");
//        }
        UpdateWrapper<ServiceInfo> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("service_id",updateServiceForm.getServiceId())
                .set("service_name",updateServiceForm.getServiceName())
                .set("service_price",updateServiceForm.getServicePrice())
                .set("service_second_id",updateServiceForm.getServiceSecondId())
                .set("service_photo",updateServiceForm.getServicePhoto())
                .set("service_status",updateServiceForm.getServiceStatus());
        int i = serviceInfoMapper.update(null, wrapper1);
        if (i < 1) {
            throw  new AppException("修改服务详情失败");
        }
    }

    @Override
    public void deleteById(Integer serviceId) {

        UpdateWrapper<ServiceInfo> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("service_id",serviceId)
                .set("deleted",1);
        int update = serviceInfoMapper.update(null, wrapper1);
        if (update<1){
            throw  new AppException("逻辑删除服务失败");
        }
    }

    //根据二级服务id查询所有服务详情
    public List<ServiceInfo> getServiceInfoBy2Id(Integer serviceSecondId) {
        List<ServiceInfo> serviceInfos = serviceInfoMapper.selectList(new QueryWrapper<ServiceInfo>().eq("service_second_id", serviceSecondId));
        if(serviceInfos==null){
            throw new AppException("没有服务信息");
        }
        return serviceInfos;
    }
}
